package customview.ike.com.customviewproject.widget.Refresh1.rotationArrow;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import customview.ike.com.customviewproject.R;
import customview.ike.com.customviewproject.widget.Refresh1.PullAbleView;
import customview.ike.com.customviewproject.widget.Refresh1.PullToRefreshLayout;
import customview.ike.com.customviewproject.widget.Refresh1.Utils;

/**
 * 实现了PullAbleView接口的view
 */

public class PullAbleListViewLayout extends LinearLayout implements PullAbleView {
    private String Tag="PullAbleListViewLayout";
    private View mRootView;
    private ListView lv;
    private RecyclerView rl;
    private List<String> data=new ArrayList<>();
    private PullAbleLinearLayout empty_view;
    private PullToRefreshLayout parent;

    public PullAbleListViewLayout(Context context) {
        this(context,null);
    }

    public PullAbleListViewLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PullAbleListViewLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mRootView=View.inflate(context, R.layout.layout_pullable,this);
        lv= (ListView) mRootView.findViewById(R.id.lv);
        rl= (RecyclerView) mRootView.findViewById(R.id.rl);
        empty_view= (PullAbleLinearLayout) findViewById(R.id.empty_view);
        
        rl.setVisibility(GONE);
        lv.setVisibility(GONE);
        initData();
    }


    private void initData() {
        for (int i=0;i<50;i++) {
            data.add("我是数据:"+i);
        }
        //lv.setAdapter(new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,data));
        LinearLayoutManager manager=new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rl.setLayoutManager(manager);
       rl.setAdapter(new RecycleAdapter());
    }

    @Override
    public boolean canPullDown() {
//        listView的判断规则
//        if (lv.getAdapter()==null||!canChildScrollUp(lv)||lv.getAdapter().getCount()==0) {
//            return true;
//        }
        //return false;
        //RecyclerView的判断规则
        if (rl.getVisibility()==VISIBLE){
            if (rl.getAdapter()==null){
                if (getParent() instanceof PullToRefreshLayout){
                    PullToRefreshLayout parent= (PullToRefreshLayout) getParent();
                    int scrollY = parent.getScrollY();
                    if (scrollY==0){
                        return true;
                    }
                }
            }else {
                LinearLayoutManager manager= (LinearLayoutManager) rl.getLayoutManager();
                int firstVisibleItemPosition = manager.findFirstVisibleItemPosition();
                View firstChild = rl.getChildAt(0);
                if (firstVisibleItemPosition==0&&firstChild.getTop()==0){
                    return true;
                }
            }
        }else {
            if (getParent() instanceof PullToRefreshLayout){
                parent = (PullToRefreshLayout)getParent();
            }
            if (parent!=null){
                if ( parent.getScrollY()==0&&parent.getScrollDirection()== PullToRefreshLayout.ScrollDirection.DOWN){
                    return true;
                }else {
                    Log.e(Tag,"parent.getScrollY():"+parent.getScrollY()+",getScrollDirection："+parent.getScrollDirection());
                }
            } else {
                Log.e(Tag,"执行了");
            }
        }
        

        return false;
    }

    @Override
    public boolean canPullUp() {
        //listView的判断规则
//        if (lv.getAdapter()==null||!canChildScrollDown(lv)||lv.getAdapter().getCount()==0){
//            Log.e(Tag,"canChildScrollUp:"+canChildScrollDown(lv));
//            return true;
//        }
//        return false;
        //RecyclerView的判断规则
        if (rl.getVisibility()==VISIBLE){
            if (rl.getAdapter()==null){
                if (getParent() instanceof PullToRefreshLayout){
                    PullToRefreshLayout parent= (PullToRefreshLayout) getParent();
                    int scrollY = parent.getScrollY();
                    if (scrollY==0){
                        return true;
                    }
                }
            }else {
                boolean viewAtBottom = Utils.isViewAtBottom(rl);

                if (viewAtBottom){
                    return true;
                }
            }  
        }else {
            if (getParent() instanceof PullToRefreshLayout){
                parent = (PullToRefreshLayout) getParent();
            }
            if (parent!=null){
                if ( parent.getScrollY()==0&&parent.getScrollDirection()== PullToRefreshLayout.ScrollDirection.UP){
                    return true;
                }
            }
        }
       
       

        return false;
    }
    /**
     * 判断listview是否在底部
     * @return
     */
    public boolean isListViewAtBottom(){
        boolean canScrollUp = ViewCompat.canScrollVertically(lv, 1);
        if (canScrollUp){
            return false;
        }
        return true;
    }

    /**
     * 判断子View是否到达顶部
     * @param mTarget
     * @return
     */
    public boolean canChildScrollUp(View mTarget) {
       
        if (android.os.Build.VERSION.SDK_INT < 14) {
            if (mTarget instanceof AbsListView) {
                final AbsListView absListView = (AbsListView) mTarget;
                return absListView.getChildCount() > 0
                        && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
                        .getTop() < absListView.getPaddingTop());
            } else {
                return ViewCompat.canScrollVertically(mTarget, -1) || mTarget.getScrollY() > 0;
            }
        } else {
            return ViewCompat.canScrollVertically(mTarget, -1);
        }
    }
    /**
     * 判断子View是否到达顶部
     * @param mTarget
     * @return
     */
    public boolean canChildScrollDown(View mTarget) {

        if (android.os.Build.VERSION.SDK_INT < 14) {
            return ViewCompat.canScrollVertically(mTarget, 1) || mTarget.getScrollY() > 0;
        } else {
            return ViewCompat.canScrollVertically(mTarget, 1);
        }
    }
    /**
     * 判断listview是否在顶部
     *
     * @return
     */
    public boolean isListViewAtTop() {
        if (lv.getAdapter() != null) {
            int firstVisiblePosition = lv.getFirstVisiblePosition();
            View child = lv.getChildAt(0);
            if (firstVisiblePosition == 0 && child != null && child.getTop() == 0) {
                return true;
            }
            return false;
        }
        return false;
    }
    class RecycleAdapter extends RecyclerView.Adapter{

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            ViewHolder viewHolder=new ViewHolder(View.inflate(getContext(),android.R.layout.simple_list_item_1,null));
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ViewHolder mHolder= (ViewHolder) holder;
            mHolder.tv.setText(data.get(position));
        }

        @Override
        public int getItemCount() {
            return data.size();
        }
    }
    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tv;
        public ViewHolder(View itemView) {
            super(itemView);
            tv= (TextView) itemView.findViewById(android.R.id.text1);
        }
    }

   
}
